package cn.kent.heima.base.directMemory;

import java.nio.ByteBuffer;
import java.util.ArrayList;

/**
 * 演示直接内存溢出
 * 底层原理: 使用了Cleaner虚引用来检测ByteBuffer对象,
 *          当ByteBuffer对象被垃圾回收时,会由ReferenceHandler线程通过Cleaner方法调用freeMemory来释放直接内存
 */
public class Demo_11 {
    static int _100Mb = 1024 * 1024 * 100;

    public static void main(String[] args) {
        ArrayList<ByteBuffer> list = new ArrayList<>();
        int i = 0;
        try {
            while (true) {
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100Mb);
                list.add(byteBuffer);
                i++;
            }
        } finally {
            System.out.println(i);
        }
    }
}
